﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimeAxis.Admin.Manager
{
    public partial class DocEdit : System.Web.UI.Page
    {
        public Models.Doc tempdoc = new Models.Doc();
        public List<Models.Category> categorys = null;
        public List<Models.DocCategory> dcs = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
                tempdoc.Id = Convert.ToInt32(Request.QueryString["docid"]);
                tempdoc = Impls.ImplDoc.GetInstance(db).GetModel(tempdoc.Id);

                Interfaces.ICategory iCategory = Impls.ImplCategory.GetInstance(db);
                categorys = iCategory.GetList();

                Models.DocCategory dc = new Models.DocCategory();
                dc.DocId = tempdoc.Id;
                dcs = Impls.ImplDocCategory.GetInstance(db).GetSomeObject(dc);

                db.Commit();
                db.Dispose();
                this.TextBox1.Text = tempdoc.Content;
                this.UserName.Text = tempdoc.Title;
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            int loginUser = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            Models.Doc doc = new Models.Doc();
            doc.Id = Convert.ToInt32(Request.QueryString["docid"]);
            doc = Impls.ImplDoc.GetInstance(db).GetModel(doc.Id);
            if (doc.UsersId == loginUser || Impls.ImplUsers.GetInstance(db).GetList().LastOrDefault().Id == loginUser)
            {
                doc.Content = this.TextBox1.Text;
                doc.Title = this.UserName.Text;
                db.Edit(doc);
            }
            else
            {
                this.Page.RegisterClientScriptBlock("login", "<script type=\"text/javascript\">alert('对不起，您不具备修改的权限')</script>");
            }

            Models.DocCategory dc = new Models.DocCategory();
            dc.DocId = doc.Id;
            dcs = Impls.ImplDocCategory.GetInstance(db).GetSomeObject(dc);
            foreach (Models.DocCategory d in dcs)
            {
                Impls.ImplDocCategory.GetInstance(db).Delete(d);
            }

            string[] names = Request.Form["querycategory"].Split(new char[] { ',' });
            foreach (string s in names)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    Models.DocCategory dg = new Models.DocCategory();
                    dg.DocId = doc.Id;
                    dg.CategoryId = Convert.ToInt32(s);
                    Impls.ImplDocCategory.GetInstance(db).Insert(dg);
                }
            }



            db.Commit();
            db.Dispose();

            Response.Redirect("../MyList.aspx");
        }

        protected void LoginButton_Click2(object sender, EventArgs e)
        {
            Response.Redirect("../MyList.aspx");
        }
    }
}